﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using System.Configuration.Provider;
using System.Configuration;
using System.IO;
using System.Windows.Forms;

namespace HomeDmsPageStorageProviders
{
    public class FilePageStorageProvider : PageStorageProvider
    {
        public const string RootPathParamName = "rootPath";

        private string _rootPath;

        public string RootPath
        {
            get { return _rootPath; }
            set { _rootPath = value; }
        }

        public override string Description
        {
            get
            {
                return "File System based page storage provider";
            }
        }

        public override string Name
        {
            get
            {
                return "FilePageStorageProvider";
            }
        }

        public override void Initialize(string name, NameValueCollection config)
        {
            base.Initialize(name, config);
            _rootPath = config[RootPathParamName];
            if (String.IsNullOrEmpty(_rootPath))
                _rootPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Bind-n-find");
        }

        public override void RetrievePage(string uri, out byte[] bytes)
        {
            // TODO:
            throw new NotImplementedException();
        }

        public override void UpdatePage(string uri, string name, byte[] bytes)
        {
            // TODO:
            throw new NotImplementedException();
        }

        public override string CreatePage(string name, byte[] bytes)
        {
            // TODO:
            throw new NotImplementedException();
        }

        public override void DeletePage(string uri)
        {
            // TODO:
            throw new NotImplementedException();
        }

        public override DialogResult ShowConfigurationDialog(IWin32Window owner)
        {
            using (FilePageStorageProviderConfigForm cform = new FilePageStorageProviderConfigForm())
            {
                cform.textBox1.Text = _rootPath;
                DialogResult result = cform.ShowDialog();
                if (result == DialogResult.OK)
                    _rootPath = cform.textBox1.Text;
                return result;
            }
        }

        public override void WriteConfiguration(NameValueCollection nameValueCollection)
        {
            base.WriteConfiguration(nameValueCollection);
            nameValueCollection[RootPathParamName] = _rootPath;
        }
    }
}
